﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;


namespace Web.Shalo.BanPMS1.Admin
{
    public partial class QLNhapHang : System.Web.UI.Page
    {
        int sott = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["manv"] == null || Session["matkhau"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    DSCTNH();
                    MultiView1.ActiveViewIndex = 0;
                }
            }
        }
        public void DSCTNH()
        {
            CTNHController obj=new CTNHController();
            GVCtnh.DataSource = obj.bllDSCTNH(sott);
            GVCtnh.DataBind();
        }

        protected void btnNhapHang_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            labNgayNhap.Text = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();

            NhaccController objncc = new NhaccController();
            ddlNhaCC.DataSource = objncc.bllDSNhacc("");
            ddlNhaCC.DataTextField = "TenNCC";
            ddlNhaCC.DataValueField = "MaNCC";
            ddlNhaCC.DataBind();

            MatHangController objmh = new MatHangController();
            ddlMatHang.DataSource = objmh.bllHTMatHang();
            ddlMatHang.DataTextField = "TenMH";
            ddlMatHang.DataValueField = "MaMH";
            ddlMatHang.DataBind();

            btnSua.Visible = false;
            labSoTT.Visible = false;
            labNgayNhap2.Visible = false;
            labNCC.Visible = false;
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            CTNHController obj = new CTNHController();
            string ngaynhap = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/" + DateTime.Now.Year.ToString();
            int soluong= Convert.ToInt16(txtSoLuong.Text);
            long giamua=Convert.ToInt64(txtGiaMua.Text);
            bool res = obj.bllThemCTNH(ddlMatHang.SelectedValue.ToString(), ddlNhaCC.SelectedValue.ToString(), ngaynhap, soluong, giamua);
            ThongBao(res);
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            CTNHController obj = new CTNHController();
            int soluong = Convert.ToInt16(txtSoLuong.Text);
            long giamua = Convert.ToInt64(txtGiaMua.Text);
            bool res = obj.bllSuaCTNH(soluong, giamua, Convert.ToInt16(labSoTT.Text));
            ThongBao(res);
        }

        public void ThongBao(bool res)
        {
            if (res)
            {
                Response.Redirect("QLNhapHang.aspx");
            }
            else
            {
                Page.RegisterClientScriptBlock("mes", "<script language='javascript'>alert('Thất Bại')</script>");
            }
          

            
        }

        protected void GVCtnh_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            CTNHController obj = new CTNHController();
          
            MultiView1.ActiveViewIndex = 1;
            labSoTT.Text = GVCtnh.Rows[e.NewSelectedIndex].Cells[0].Text;
            int stt = Convert.ToInt16(labSoTT.Text);
            DataTable tb = obj.bllDSCTNH(stt);
            labMatHang.Text = tb.Rows[0]["TenMH"].ToString();
            labNCC.Text = tb.Rows[0]["TenNCC"].ToString();
            txtSoLuong.Text=tb.Rows[0]["SoLuong"].ToString();
            txtGiaMua.Text = tb.Rows[0]["GiaMua"].ToString();
            labNgayNhap2.Text=tb.Rows[0]["NgayNhap"].ToString();


            labNgayNhap.Visible = false;
            ddlMatHang.Visible = false;
            ddlNhaCC.Visible = false;
            btnThem.Visible = false;
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("QLNhapHang.aspx");
        }

        protected void GVCtnh_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool res = false;
            CTNHController obj = new CTNHController();
            sott = Convert.ToInt16(GVCtnh.Rows[e.RowIndex].Cells[0].Text);
            res = obj.bllXoaCTNH(sott);
            ThongBao(res);
        }

        protected void GVCtnh_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVCtnh.PageIndex = e.NewPageIndex;
            DSCTNH();

        }
        
    }
}